package edu.cs346.group5.logic;

import java.sql.Date;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import edu.cs346.group5.backend.Database;
import edu.cs346.group5.datastores.Topic;
import edu.cs346.group5.datastores.User;

public class Home {
	
	public static Topic[] GetAllTopics(){
		List<HashMap<String, Object>> results = null;
		try {
			results = Database
					.select("Select * From Items i INNER JOIN Posts p ON i.id = p.item_id INNER JOIN Users u ON u.id = p.posted_by Order By posted_date DESC Limit 50");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Topic[] topics = new Topic[results.size()];
		for (int i = 0; i < topics.length; i++) {
			Topic temp = new Topic();
			temp.setCategoryId((int) results.get(i).get("category"));
			temp.setDescription(results.get(i).get("description").toString());
			temp.setImageUrl(results.get(i).get("image").toString());
			temp.setReferenceURL(results.get(i).get("reference_url").toString());
			temp.setTopicName(results.get(i).get("content").toString());
			temp.setItemId((int) results.get(i).get("item_id"));
			temp.setPostDate((Date) results.get(i).get("posted_date"));
			temp.setPostedBy(results.get(i).get("first_name").toString() + " " + results.get(i).get("last_name").toString());
			temp.setPostId((int)results.get(i).get("id"));
			temp.setTotalPoints((int)results.get(i).get("total_points"));
			temp.setNumberOfVotes((int)results.get(i).get("number_of_votes"));
			topics[i] = temp;
		}
		return topics;
	}
	
	public static Topic GetTopicByNameDate(Topic topic){
		List<HashMap<String, Object>> results = null;
		
		try {
			results = Database.select("Select * From Items i INNER JOIN Posts p ON i.id = p.item_id Where p.content = '" + topic.getTopicName() + "' AND p.posted_date = '" + topic.getPostDate() + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Topic newTopic = new Topic();
		newTopic.setCategoryId((int) results.get(0).get("category"));
		newTopic.setDescription(results.get(0).get("description").toString());
		newTopic.setImageUrl(results.get(0).get("image").toString());
		newTopic.setReferenceURL(results.get(0).get("reference_url").toString());
		newTopic.setTopicName(results.get(0).get("content").toString());
		newTopic.setRating(results.get(0).get("rating") == null ? 0 : (float)results.get(0).get("rating"));
		newTopic.setPostId((int)results.get(0).get("id"));
		newTopic.setTotalPoints((int)results.get(0).get("total_points"));
		newTopic.setNumberOfVotes((int)results.get(0).get("number_of_votes"));
		newTopic.setPostDate((Date)results.get(0).get("posted_date"));
		newTopic.setItemId((int)results.get(0).get("item_id"));
		return newTopic;
	}
	
	public static User GetUserByEmail(String email){
		List<HashMap<String, Object>> results = null;
		
		try {
			results = Database.select("Select * From Users Where email = '" + email + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		User user = new  User();
		user.Email = results.get(0).get("email").toString();
		user.RegisterDate = (java.util.Date) results.get(0).get("register_date");
		user.FirstName = results.get(0).get("first_name").toString();
		user.LastName = results.get(0).get("last_name").toString();
		user.Id = (int) results.get(0).get("id");
		user.isAdmin= user.Email.equals("admin");
		user.ProfilePicture = results.get(0).get("profile_picture").toString();
		return user;
	}

}
